import asyncio
import logging
from datetime import datetime
from aiowebsocket.converses import AioWebSocket

'''测试用第三方框架抓取ws'''


async def startup(uri):
    async with AioWebSocket(uri) as aws:
        converse = aws.manipulator
        # 客户端给服务端发送消息
        message = '{"action":"join","uuid":"","type":"room","room_id":135565599}'
        '''
            uuid去掉- 就是MD5加密算法 ， 但是排序可能为120种。 只能通过JS解密去寻找加密源头。
        '''
        # 3f041225-c8be-4634-987f-b41874a506b2
        # 加入
        print(123123123123)
        await converse.send(message)
        while True:
            print('{time}-Client send: {message}'
                  .format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message=message))
            mes = await converse.receive()
            if mes == '❤️':
                print('接收到爱心1')
                await converse.send('❤️')
            elif mes == '❤':
                print('接收到爱心2')
                await converse.send('❤')
        print('{time}-Client receive: {rec}'
                  .format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), rec=mes))


if __name__ == '__main__':
    remote = 'wss://fm.missevan.com:3016/ws'

    try:
        asyncio.get_event_loop().run_until_complete(startup(remote))
    except KeyboardInterrupt as exc:
        logging.info('Quit.')
